<?php
	class MessageView
	{
		// Membervariables
		private $_navView = null;
		private $_userHandler = null;

		// Constants
		const MESSAGETEXTAREACOLS = 96;
		const MESSAGETEXTAREAROWS = 24;
		const TOPIC = "Topic";
		const TO = "To";
		const MESSAGECONTENT = "MessageContent";
		const SEND = "Send";
		
		public function __construct(\UserHandler $userHandler, \NavigationView $navView)
		{
			$this->_navView = $navView;
			$this->_userHandler = $userHandler;
		}
		
		// Returns the messaging menu.
		public function MessageMenu()
		{
			$ret = "<br />";
			if($this->TriedToLoadMessaging() == true)
			{
				$ret .= "<a href='" . $this->_navView->GetNewMessageLink() ."'>" . NavigationView::LINKTEXTNEWMESSAGE . "</a><br />";
			}
			else 
			{
				$ret .= "<a href='" . $this->_navView->GetMessagingAdress() . "'>" . NavigationView::LINKTEXTBACKTOMESSAGING . "</a><br />";
			}
			
			return $ret;
		}
		
		// Returns the form to write a new message.
		public function WriteNewMessageForm()
		{
			$ret = "<form method='post' action='" . $this->_navView->GetNewMessageLink() . "'>" .
				"Topic: <input id='username' name='" . MessageView::TOPIC . "' type='text' /><br />" .
				"To: <input id='password' name='" . MessageView::TO . "' type='text' /><br />" .
				"<textarea rows='" . $this::MESSAGETEXTAREAROWS . "' cols='" . $this::MESSAGETEXTAREACOLS . "' name='" . MessageView::MESSAGECONTENT . "'/></textarea><br />" .
				"<input id='login' name='" . MessageView::SEND . "' type='submit' value='" . MessageView::SEND . "' />" .
			"</form>";
			return $ret;
		}
		
		// Check wether the user tried to send the new message.
		public function TriedToSendMessage()
		{
			if (isset($_POST[MessageView::SEND]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Check wether the user tried to load their inbox.
		public function TriedToLoadMessaging()
		{
			if (isset($_GET[NavigationView::IWANTTOLOADMESSAGING]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Check wether the user tried to read a message.
		public function TriedToReadMessage()
		{
			if (isset($_GET[NavigationView::IWANTTOREADMESSAGE]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Checks wether the user tried to access the form to write a new message.
		public function TriedToWriteNewMessage()
		{
			if (isset($_GET[NavigationView::IWANTTOWRITEANEWMESSAGE]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Print all the messages in the users inbox.
		public function PrintMessageList($messageList)
		{
			$ret = "<b>Messages</b><br />";

			foreach($messageList as $message)
			{
				if ($message->readstatus == 0)
				{
					$ret .= "<b>" . $this->GenerateMessageLink($message) . "</b><br />";
				}
				else 
				{
					$ret .= $this->GenerateMessageLink($message) . "<br />";
				}
			}
			
			return $ret;
		}
		
		// Prints a specified message.
		public function PrintMessage(\Message $message)
		{
			$ret = "<br />";
			
			$ret .= $message->topic . "<br />" .
			"From: " . $message->senderusername . "<br />" .
			"Sent: " . $message->senddate . "<br />" .
			"<br />" . $message->messagecontent;
			
			return $ret;
		}		
		
		// Generates the url when a user clicks a message in the inbox.
		public function GenerateMessageLink(\Message $message)
		{
			return "<a href ='" . $this->_navView->GetFormActionLink() . "?MessageId=" . $message->messageid . "'>$message->topic</a>";
		}
		
		// Returns the specified messageid.
		public function GetMessageId()
		{
			return $_GET[NavigationView::IWANTTOREADMESSAGE];
		}
		
		// Returns the topic info.
		public function GetTopic()
		{
			return $_POST[MessageView::TOPIC];
		}
		
		// Returns the reciever info.
		public function GetReciever()
		{
			return $_POST[MessageView::TO];
		}
		
		// Returns the reciever id.
		public function GetRecieverId()
		{
			$username = $_POST[MessageView::TO];
			$user = $this->_userHandler->GetUser($username);
			
			$id = $user->id;
			return $id;
		}
		
		// Returns the content info.
		public function GetContent()
		{
			return $_POST[MessageView::MESSAGECONTENT];
		}
	}